package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("report")
public class ReportController {

    @Reference
    private ReportService reportService;

    @Reference
    private MemberService memberService;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("getMemberReport")
    public Result getMemberReport() {

        try {
            Map map = memberService.getMemberReport();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }

    //会员性别统计
    @RequestMapping("getMemberSex")
    public Result getMemberSex() {
        try {
            Map map = memberService.getMemberSex();
            return new Result(true, "统计性别查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "统计性别查询失败");
        }
    }

    //会员年龄统计
    @RequestMapping("getMemberAge")
    public Result getMemberAge() {
        try {
            Map map = memberService.getMemberAge();
            return new Result(true, "统计年龄查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "统计年龄查询失败");
        }
    }

    @RequestMapping("/memberReport.do")
    public Result memberReport(String dateCondition) {
        try {
            String start = null;
            String end = null;
            if (dateCondition != null) {
                String[] dateArr = dateCondition.split(",");
                if (dateArr.length == 2) {
                    start = dateArr[0];
                    end = dateArr[1];
                }
            }
            Map map = memberService.getMemberReportByMonths(start, end);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL, null);
        }
    }

    /**
     * @Author: hyy
     * @Description:套餐占比
     * @Date: 18:54 2020/7/2
     * @param:
     * @return: com.itheima.entity.Result
     * @Version: 1.0
     */
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        try {
            Map map = setmealService.getSetmealReport();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    /**
     * @Author: hyy
     * @Description:查询运营数据
     * @Date: 21:19 2020/7/2
     * @param:
     * @return: com.itheima.entity.Result
     * @Version: 1.0
     */
    @RequestMapping("/getBusinessReport")
    public Result getBusinessReport() {
        try {
            Map map = reportService.getBusinessReport();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    /**
     * @Author: hyy
     * @Description:到处excel数据
     * @Date: 21:36 2020/7/2
     * @param:
     * @return: com.itheima.entity.Result
     * @Version: 1.0
     */
    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpSession session, HttpServletResponse response) {
        try {
            //获取运营数据
            Map result = reportService.getBusinessReport();
            /**
             * reportDate,//今日日期
             * todayNewMember,//今日新增会员数
             * totalMember,//总会员数
             * thisWeekNewMember,//本周新增会员数
             * thisMonthNewMember,//本月新增会员数
             * todayOrderNumber,//今日预约数
             * todayVisitsNumber,//今日到诊数
             * thisWeekOrderNumber,//本周预约数
             * thisWeekVisitsNumber,//本周到诊数
             * thisMonthOrderNumber,//本月预约数
             * thisMonthVisitsNumber,//本月到诊数
             * hotSetmeal:热门套餐4个
             */
            //取出返回的数据，准备将报表中的数据写入到excel中
            String reportDate = (String) result.get("reportDate");
            Integer todayNewMember = (Integer) result.get("todayNewMember");
            Integer totalMember = (Integer) result.get("totalMember");
            Integer thisWeekNewMember = (Integer) result.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) result.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) result.get("todayOrderNumber");
            Integer todayVisitsNumber = (Integer) result.get("todayVisitsNumber");
            Integer thisWeekOrderNumber = (Integer) result.get("thisWeekOrderNumber");
            Integer thisWeekVisitsNumber = (Integer) result.get("thisWeekVisitsNumber");
            Integer thisMonthOrderNumber = (Integer) result.get("thisMonthOrderNumber");
            Integer thisMonthVisitsNumber = (Integer) result.get("thisMonthVisitsNumber");
            List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");

            //01获取模板的数据
            String templatePath = session.getServletContext().getRealPath("template") + File.separator + "report_template.xlsx";
            //02:读取模板获取excel对象
            XSSFWorkbook workbook = new XSSFWorkbook(templatePath);

            //获取工作表
            XSSFSheet sheet = workbook.getSheetAt(0);
            //获取第二行
            XSSFRow row = sheet.getRow(2);
            row.getCell(5).setCellValue(reportDate);//日期

            row = sheet.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);//新增会员数
            row.getCell(7).setCellValue(totalMember);//总会员数

            row = sheet.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);//本周新增会员数
            row.getCell(7).setCellValue(thisMonthNewMember);//本月新增会员数

            row = sheet.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);//今日预约数
            row.getCell(7).setCellValue(todayVisitsNumber);//今日到诊数

            row = sheet.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);//本周到诊数
            row.getCell(7).setCellValue(thisWeekVisitsNumber);//本周预约数

            row = sheet.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);//本周到诊数
            row.getCell(7).setCellValue(thisMonthVisitsNumber);//本周预约数

            int rowNub = 12;
            for (Map map : hotSetmeal) {
                String name = (String) map.get("name");
                Long setmeal_count = (Long) map.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) map.get("proportion");//套餐占比
                row = sheet.getRow(rowNub);
                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmeal_count);
                row.getCell(6).setCellValue(proportion.toString());
                rowNub++;
            }
            //设置相应文件格式
            response.setContentType("application/vnd.ms-excel");
            //设置下载方式
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
            //获取输出流
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);//将excel相应出去
            out.flush();//清空缓冲区
            out.close();
            workbook.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
